package com.dk.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller

public class ShiroLoginController {
    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("login")
    public String getLogin(String uname, String pwd, Model model){
        /*
        * 编写shiro 认证操作
        *
        * */
        //获取 Subject
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(uname,pwd);
        //登录方法
        try {
                subject.login(token);
                //登录成功
            return "redirect:/index";
            }catch (UnknownAccountException e){
                //登录失败用户名不存在
                model.addAttribute("message","用户名不存在");
            System.out.println("用户名不存在");
            return "login";
            }catch (IncorrectCredentialsException e){
            //登录失败 密码错误
            System.out.println("密码错误");
            model.addAttribute("message","密码错误");
            return "login";
        //叫兽1
      //叫兽2
        }

    }

}
